﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sys.WebRes.Data;
using System.IO;
using Sys.WebRes.Publish.Areas.Admin.Models;

namespace Sys.WebRes.Publish.Areas.Admin.Controllers
{
    public class FileSysController : Controller
    {
        /// <summary>
        /// Upload文件夹中查询文件
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string typeFolder, string folderName)
        {
            var uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SysConfig.UploadFolder);
            var currentFolder = uploadFolder;
            if (!string.IsNullOrEmpty(typeFolder))
            {
                currentFolder = Path.Combine(uploadFolder, typeFolder);
            }

            var result = new List<FileSysModel>();
            
            //文件夹列表显示
            if (string.IsNullOrEmpty(typeFolder) || string.IsNullOrEmpty(folderName))
            {
                string[] folders = System.IO.Directory.GetDirectories(currentFolder);
                if (folders != null)
                {
                    foreach (var fd in folders)
                    {
                        var fdName = Path.GetFileName(fd);
                        var model = new FileSysModel() { IsFolder = true };
                        model.FileName = fdName;
                        model.Title = fdName;
                        model.Type = "dir";
                        model.Path = string.IsNullOrEmpty(typeFolder) ? 
                                        SysConfig.UploadFolder
                                        : Path.Combine(typeFolder, fdName);

                        result.Add(model);
                    }
                }
            }
            else //指定的文件夹下的文件列表
            {
                currentFolder = Path.Combine(currentFolder, folderName);
                string[] files = System.IO.Directory.GetFiles(currentFolder);
                if (files != null)
                {
                    var innerPath = Path.Combine(typeFolder, folderName);
                    foreach (var path in files)
                    {
                        var fileName = Path.GetFileName(path);
                        var model = new FileSysModel() { IsFolder = false };

                        model.FileName = fileName;
                        model.Title = Path.GetFileNameWithoutExtension(fileName);
                        model.Type = Path.GetExtension(fileName);
                        model.Type = string.IsNullOrEmpty(model.Type) ? "file" : model.Type.Substring(1);
                        model.Path = Path.Combine(innerPath, fileName);

                        result.Add(model);
                    }
                }
            }
            
            ViewData.Add("typeFolder", typeFolder);

            return View(result);
        }

        public ActionResult Index2()
        {

            return View();
        }
    }
}
